home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-06-19 | 17.6 KB | 644 lines |
- package symantec.itools.awt;
-
-
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.FontMetrics;
- import java.awt.Font;
- import java.awt.Graphics;
- import java.awt.Rectangle;
-
- // 01/29/97 TWB Integrated changes from Macintosh
- // 02/27/97 RKM Merged in Scott's changes
-
- /**
- * Creates a text string that initiates an action when clicked. A LabelButton
- * appears outlined when it possesses input focus. This indicates that the
- * component can receive a user event. When the user clicks the button, it
- * appears depressed to indicate that it has detected the click event.
- * <p>
- * When a LabelButton is disabled, it is ægrayed out.Æ This indicates that
- * it can not receive user input.
- * <p>
- * The text string accepts clicked events automatically by default.
- * <p>
- * Use LabelButton to:
- * <UL>
- * <DT>╖ Accept or yield focus.
- * <DT>╖ Respond to a user event.
- * <DT>╖ Send a train of action events to another component.
- * <p>
- * To define an interaction with another component, use the Interaction Wizard.
- * <p>
- * @version 1.0, Nov 26, 1996
- * @author Symantec
- */
-
- public class LabelButton
- extends ButtonBase
- implements AlignStyle,
- BevelStyle
- {
- //--------------------------------------------------
- // constants
- //--------------------------------------------------
-
- /**
- * Constant indicating a drawing margin of 0 pixels around the inside of
- * the border.
- */
- public static final int INDENT_ZERO = 0;
-
- /**
- * Constant indicating a drawing margin of 1 pixel around the inside of
- * the border.
- */
- public static final int INDENT_ONE = 1;
-
- /**
- * Constant indicating a drawing margin of 2 pixels around the inside of
- * the border.
- */
- public static final int INDENT_TWO = 2;
-
-
-
- //--------------------------------------------------
- // class variables
- //--------------------------------------------------
-
-
- //--------------------------------------------------
- // member variables
- //--------------------------------------------------
-
- /**
- * The button label text.
- */
- protected String sLabelButton;
- /**
- * Indicates whether to display the label text.
- */
- protected boolean showLabel;
- private int alignStyle;
- private int bevelStyle;
- private Color color1;
- private Color color2;
- private Color textColor;
- private Color borderedColor;
- private FontMetrics fm;
- private int xTemp;
- private int yTemp;
- private int indent = INDENT_ZERO;
- private int textWidth;
- private int textAscent;
- private int textHeight;
- private boolean bOsFlag = false;
-
-
-
- //--------------------------------------------------
- // constructors
- //--------------------------------------------------
-
- /**
- * Constructs a LabelButton with black text, center aligned, raised border,
- * no text, and a zero border indent.
- */
- public LabelButton()
- {
- this("", ALIGN_CENTERED, BEVEL_RAISED, Color.black, INDENT_ZERO, true);
- }
-
- /**
- * Constructs a LabelButton with black text, ZERO indent
- * @param sText text of the LabelButton
- * @param style numeric value indicating LabelButton style, such as LabelButton.IN + LabelButton.CENTERED
- */
- /**
- * Constructs a LabelButton with black text, a zero border indent, and the
- * specified text alignment, and bevel styles.
- * @param sText text of the LabelButton
- * @param alignStyle text alignment style: ALIGN_LEFT, ALIGN_CENTERED, or ALIGN_RIGHT
- * @param bevelStyle bevel style: BEVEL_RAISED, BEVEL_LOWERED, BEVEL_LINE, or BEVEL_NONE
- */
- public LabelButton(String sText, int alignStyle, int bevelStyle)
- {
- this(sText, alignStyle, bevelStyle, Color.black, INDENT_ZERO, true);
- }
-
- /**
- * Constructs a LabelButton with a zero border indent and the specified attributes.
- * @param sText text of the LabelButton
- * @param alignStyle text alignment style: ALIGN_LEFT, ALIGN_CENTERED, or ALIGN_RIGHT
- * @param bevelStyle bevel style: BEVEL_RAISED, BEVEL_LOWERED, BEVEL_LINE, or BEVEL_NONE
- * @param color the label text color
- */
- public LabelButton(String sText, int alignStyle, int bevelStyle, Color color)
- {
- this(sText, alignStyle, bevelStyle, color, INDENT_ZERO, true);
- }
-
- /**
- * Constructs a Label3D with black label text and the specified attributes.
- * @param sText text of the LabelButton
- * @param alignStyle text alignment style: ALIGN_LEFT, ALIGN_CENTERED, or ALIGN_RIGHT
- * @param bevelStyle bevel style: BEVEL_RAISED, BEVEL_LOWERED, BEVEL_LINE, or BEVEL_NONE
- * @param indent the amount to indent around the border: INDENT_ZERO,
- * INDENT_ONE, or INDENT_TWO
- */
- public LabelButton(String sText, int alignStyle, int bevelStyle, int indent)
- {
- this(sText, alignStyle, bevelStyle, Color.black, indent, true);
- }
-
- /**
- * Constructs a Label3D with the specified attributes.
- * @param sText text of the Label3D
- * @param alignStyle text alignment style: ALIGN_LEFT, ALIGN_CENTERED, or ALIGN_RIGHT
- * @param bevelStyle bevel style: BEVEL_RAISED, BEVEL_LOWERED, BEVEL_LINE, or BEVEL_NONE
- * @param color the label text color
- * @param indent the amount to indent around the border: INDENT_ZERO,
- * INDENT_ONE, or INDENT_TWO
- * @param f show the label text if true
- */
- public LabelButton(String sText, int alignStyle, int bevelStyle, Color color, int indent, boolean f)
- {
- String sOs = System.getProperty("os.name");
-
- if (sOs.startsWith("S") || // SunOS, Solaris
- sOs.startsWith("OSF") ) // OSF
- {
- bOsFlag = true;
- setFont(new Font("Dialog", Font.PLAIN, 10));
- }
- else
- {
- bOsFlag = false;
- }
-
- Font tempFont = getFont();
- if(tempFont == null)
- {
- fm = getFontMetrics(new Font("Dialog", Font.PLAIN, 12));
- }
- else
- {
- fm = getFontMetrics(tempFont);
- }
-
- setText(sText);
- textColor = color;
- borderedColor = Color.black;
-
- setBorderIndent(indent);
- setAlignStyle(alignStyle);
- setBevelStyle(bevelStyle);
- showFocus = true;
- showLabel = f;
- }
-
- //--------------------------------------------------
- // accessor methods
- //--------------------------------------------------
-
- /**
- * Sets the alignment style.
- * @param style text alignment style: ALIGN_LEFT, ALIGN_CENTERED, or ALIGN_RIGHT
- * @see #getAlignStyle
- */
- public void setAlignStyle(int style)
- {
- alignStyle = style;
- }
-
- /**
- * Gets the current alignment style.
- * @return text alignment style: ALIGN_LEFT, ALIGN_CENTERED, or ALIGN_RIGHT
- * @see #setAlignStyle
- */
- public int getAlignStyle()
- {
- return (alignStyle);
- }
-
- /**
- * Sets the border style.
- * @param style border bevel style: BEVEL_RAISED, BEVEL_LOWERED, BEVEL_LINE, or BEVEL_NONE
- * @see #getBevelStyle
- */
- public void setBevelStyle(int style)
- {
- bevelStyle = style;
-
- switch(style)
- {
- case BEVEL_LOWERED :
- {
- color1 = Color.black;
- color2 = Color.gray;
- break;
- }
- case BEVEL_RAISED :
- {
- color1 = Color.gray;
- color2 = Color.black;
- break;
- }
- case BEVEL_LINE :
- {
- color1 = borderedColor;
- color2 = borderedColor;
- break;
- }
- default: // catches any bogus type, paint(...) relies on color1 being null
- {
- color1 = color2 = null;
- break;
- }
- }
- }
-
- /**
- * Gets the current border style.
- * @return border bevel style: BEVEL_RAISED, BEVEL_LOWERED, BEVEL_LINE, or BEVEL_NONE
- * @see #setBevelStyle
- */
- public int getBevelStyle()
- {
- return (bevelStyle);
- }
-
- /**
- * Sets the border indent amount.
- * @param indent the amount to indent around the border: INDENT_ZERO,
- * INDENT_ONE, or INDENT_TWO
- * @see #getBorderIndent
- */
- public void setBorderIndent(int indent)
- {
- if (indent < INDENT_ZERO)
- {
- this.indent = INDENT_ZERO;
- }
- else if (indent > INDENT_TWO)
- {
- this.indent = INDENT_TWO;
- }
- else
- {
- this.indent = indent;
- }
- }
-
- /**
- * Gets the current border indent amount.
- * @return the amount currently indented around the border: INDENT_ZERO,
- * INDENT_ONE, or INDENT_TWO
- * @see #setBorderIndent
- */
- public int getBorderIndent()
- {
- return indent;
- }
-
- /**
- * Sets the border color.
- * @param color color to use for the border
- */
- public void setBorderedColor(Color color)
- {
- borderedColor = color;
-
- if (bevelStyle == BEVEL_LINE)
- {
- color1 = color;
- color2 = color;
- }
-
- invalidate();
- }
-
- /**
- * Sets the label text.
- * @param sLabel the new label text
- * @see #getText
- */
- public void setText(String sText)
- {
- Font tempFont = getFont();
- if(tempFont == null)
- {
- fm = getFontMetrics(new Font("Dialog", Font.PLAIN, 12));
- }
- else
- {
- fm = getFontMetrics(tempFont);
- }
-
- sLabelButton = sText;
- textAscent = fm.getAscent();
- textHeight = fm.getHeight();
- textWidth = fm.stringWidth(sLabelButton);
- }
-
- /**
- * Gets the current label text.
- * @return the current label text
- * @see #setText
- */
- public String getText()
- {
- return sLabelButton;
- }
-
- /**
- * Sets the label text color.
- * @param color the new color for the label text
- * @see #getTextColor
- */
- public void setTextColor(Color color)
- {
- textColor = color;
- }
-
- /**
- * Gets the current label text color.
- * @return the current color of the label text
- * @see #setTextColor
- */
- public Color getTextColor()
- {
- return textColor;
- }
-
- /**
- * Sets whether the label will be shown.
- * @param f true to show the label, false otherwise
- * @see #getShowLabel
- */
- public void setShowLabel(boolean f)
- {
- showLabel = f;
- }
-
- /**
- * Gets whether the label will be shown.
- * @return true if label is shown, false otherwise
- * @see #setShowLabel
- */
- public boolean getShowLabel()
- {
- return showLabel;
- }
-
-
- //--------------------------------------------------
- // event methods
- //--------------------------------------------------
-
-
- //--------------------------------------------------
- // class methods
- //--------------------------------------------------
-
-
- //--------------------------------------------------
- // member methods
- //--------------------------------------------------
-
- /**
- * Paints this component using the given graphics context.
- * This is a standard Java AWT method which typically gets called
- * by the AWT to handle painting this component. It paints this component
- * using the given graphics context. The graphics context clipping region
- * is set to the bounding rectangle of this component and its <0,0>
- * coordinate is this component's top-left corner.
- *
- * @param g the graphics context used for painting
- * @see java.awt.Component#repaint
- * @see #update
- */
- public void paint(Graphics g)
- {
- Dimension s;
- s = size();
-
- Rectangle r;
- r = bounds();
-
- yTemp = r.height/2 + textAscent/2;
-
- switch(alignStyle)
- {
- case ALIGN_LEFT:
- {
- if (bevelStyle == BEVEL_LINE)
- {
- xTemp = 4;
- }
- else
- {
- xTemp = 8;
- }
-
- break;
- }
- case ALIGN_RIGHT:
- {
- xTemp = r.width - textWidth;
-
- if (bevelStyle == BEVEL_LINE)
- {
- xTemp -= 6;
- }
- else
- {
- xTemp -= 10;
- }
-
- break;
- }
- case ALIGN_CENTERED:
- {
- xTemp = (r.width - textWidth) / 2;
- break;
- }
- }
-
- switch(bevelStyle)
- {
- case BEVEL_RAISED:
- {
- super.paint(g);
- if (showLabel)
- {
- g.setColor(textColor);
- if(pressed)
- {
- g.drawString(sLabelButton, xTemp + pressedAdjustment, yTemp + pressedAdjustment);
- }
- else
- {
- g.drawString(sLabelButton, xTemp, yTemp);
- }
- }
- }
- break;
- case BEVEL_LOWERED:
- {
- pressed = !pressed;
- super.paint(g);
- pressed = !pressed;
-
- if (showLabel)
- {
- g.setColor(textColor);
- if(pressed)
- {
- g.drawString(sLabelButton, xTemp - pressedAdjustment, yTemp - pressedAdjustment);
- }
- else
- {
- g.drawString(sLabelButton, xTemp, yTemp);
- }
- }
- }
- break;
- case BEVEL_LINE:
- {
- g.setColor(getForeground());
- g.drawRect(0 + indent, 0 + indent, s.width - 1 - indent - indent, s.height - 1 - indent - indent);
- }
- case BEVEL_NONE:
- {
- if (showLabel)
- {
- g.setColor(textColor);
- g.drawString(sLabelButton, xTemp, yTemp);
- }
-
- if(showInfoTip)
- {
- if (doInfoTip)
- {
- drawInfoTip();
- }
- }
- }
- }
- }
-
- /**
- * Sets this component's text font.
- * This is a standard Java AWT method which gets called to change
- * the font used for drawing text in this component.
- *
- * @param f the new font to use for drawing text
- * @see java.awt.Component#getFont
- */
- public void setFont(Font f)
- {
- super.setFont(f);
- fm = getFontMetrics(getFont());
- textAscent = fm.getAscent();
- textHeight = fm.getHeight();
- textWidth = fm.stringWidth(sLabelButton);
- }
-
- /**
- * Returns the recommended dimensions to properly display this component.
- * This is a standard Java AWT method which gets called to determine
- * the recommended size of this component.
- *
- * @see #minimumSize
- */
- public Dimension preferredSize()
- {
- Dimension s = size();
- Dimension m = minimumSize();
-
- return new Dimension(Math.max(s.width, m.width), Math.max(s.height, m.height));
- }
-
- /**
- * Returns the minimum dimensions to properly display this component.
- * This is a standard Java AWT method which gets called to determine
- * the minimum size of this component.
- *
- * @see #preferredSize
- */
- public Dimension minimumSize()
- {
- Dimension min;
- Font f;
-
- min = new Dimension(18, 10);
- f = getFont();
-
- if (f == null)
- {
- if (bOsFlag)
- {
- min.height = 29;
- }
- }
- else
- {
- min.width = textWidth + 18;
- min.height = textHeight + 10;
-
- if (bOsFlag && min.height < 29)
- {
- min.height = 29;
- }
- }
-
- return min;
- }
-
- /**
- * Moves and/or resizes this component.
- * This is a standard Java AWT method which gets called to move and/or
- * resize this component. Components that are in containers with layout
- * managers should not call this method, but rely on the layout manager
- * instead.
- *
- * @param x horizontal position in the parent's coordinate space
- * @param y vertical position in the parent's coordinate space
- * @param width the new width
- * @param height the new height
- */
- public synchronized void reshape(int x, int y, int width, int height)
- {
- super.reshape(x, y, width, height);
-
- if (!isValid())
- {
- repaint();
- }
- }
-
- /**
- * Handles redrawing of this component on the screen.
- * This is a standard Java AWT method which gets called by the Java
- * AWT (repaint()) to handle repainting this component on the screen.
- * The graphics context clipping region is set to the bounding rectangle
- * of this component and its <0,0> coordinate is this component's
- * top-left corner.
- * Typically this method paints the background color to clear the
- * component's drawing space, sets graphics context to be the foreground
- * color, and then calls paint() to draw the component.
- *
- * It is overridden here to eliminate the unneeded repainting of the background.
- *
- * @param g the graphics context
- * @see java.awt.Component#repaint
- * @see #paint
- */
- public void update(Graphics g)
- {
- paint(g);
- }
- }
-